# coding:utf-8
import sys
sys.path.append('D:\\test\\Appium2021')
from util.dos_cmd import DosCmd
class Port:
    def port_is_used(self,port_num):
        '''
        判断端口是否被占用
        '''
        flag = None
        self.dos = DosCmd()
        command = 'netstat -ano | findstr '+str(port_num)
        result = self.dos.excute_cmd_result(command)
        if len(result)>0:
            flag = True
        else:
            flag = False
        return flag
    def create_port_list(self,start_port,devices_list):
        '''
        生成可用端口
        :param start_port:
        :param devices_list:
        :return:
        '''
        port_list = []
        if devices_list != None:
            while len(port_list) != len(devices_list):
                if self.port_is_used(start_port) != True:
                    port_list.append(start_port)
                start_port =start_port +1
            return port_list
        else:
            print("生成可用端口失败")
            return None



if __name__ == '__main__':

    port = Port()
    print(sys.path)
    # print(port.port_is_used('4725'))
    li = [1,2,3,4,5]
    print(port.create_port_list(4723,li))
